<?php 
class View {
    const DEFAULT_BOX = 'DEFAULT_BOX';
    
    private $view_name;
    //private $children;
    
    private $views = array();
    private $data = array();
    
    public function __construct($view_name) {
        $view_name = trim($view_name);
        if (empty($view_name)) {
            tos_die();
        }
        $this->view_name = $view_name;
    }
    
    
    public function get_view_name() {
        return $this->view_name;
    }
    
    
    public function add_view($view, $box_name = '') {
        if ($view instanceof View) {
            $view_name = $view->get_view_name();
            $box_name = trim($box_name);
            $box_name = (empty($box_name)) ? self::DEFAULT_BOX : $box_name;
            
            $this->views[$box_name][$view_name] = $view;
            
        } else {
            tos_die('Error data type');
        }
    }
    
    
    /*
    public function add_box($view) {
        if ($name = $view->get_name()) {
            $this->boxes[$name] = $view;
        }
    }
    
    
    
    public function add_view($view) {
        if ($name = $view->get_name()) {
            $this->children[$name] = $view;
        }
    }*/
    
    public function has_data($key) {
        return isset($this->data[$key]);
    }
    
    public function add_data($key, $value) {
        $this->data[$key] = $value;
    }
    
    
    
    public function add_datas($data) {
        $this->data = array_merge($data, $this->data);
    }
    
    
    
    protected function write($key) {
        if (isset($this->data[$key])) {
            echo $this->data[$key];
        }
    }
    
    protected function write_date($key) {
        if (isset($this->data[$key])) {
            $v = (int) $this->data[$key];
            echo date("Y-m-d H:i:s", $v);
        }
    }
    
    public function display($out_type = 'html') {
        $out_type = trim($out_type);
        if (empty($out_type)) {
            tos_die();
        }
        
        $site_theme_dir = SITE_DIR . '/theme';
        if (is_dir($site_theme_dir)) {
            $file = "{$site_theme_dir}/{$this->view_name}.{$out_type}.php";
            if (file_exists($file)) {
                include $file;
                return true;
            }
        }
        
        $custom_theme_dir = SYS_DIR . '/theme/' . tos_get_config('theme');
        if (is_dir($custom_theme_dir)) {
            $file = "{$custom_theme_dir}/{$this->view_name}.{$out_type}.php";
            if (file_exists($file)) {
                include $file;
                return true;
            }
        }
        
        $sys_theme_dir = SYS_DIR .'/theme/sys';
        if (is_dir($sys_theme_dir)) {
            $file = "{$sys_theme_dir}/{$this->view_name}.{$out_type}.php";
            if (file_exists($file)) {
                include $file;
                return true;
            }
        }
        tos_die("cannot find view {$this->view_name}.{$out_type} ");
        return false;
        
        /*$theme = tos_get_config('theme');
        $theme_dir = SYS_DIR . "/theme/$theme";
        if ( ! is_dir($theme_dir)) {
            tos_die();
        }
        
        $file = "{$theme_dir}/{$this->view_name}.{$out_type}.php";
        if ( ! file_exists($file)) {
            $sys_file = SYS_DIR . "/theme/sys/{$this->view_name}.{$out_type}.php";
            if ( ! file_exists($sys_file)) {
                tos_die("$file cannot found.");
            }
            $file = $sys_file;
        }
        
        include $file;*/
    }
    
    
    
    /*public function display_blocks($out_type = 'html') {
        foreach ($this->blocks as $view) {
            $view->display($out_type);
        }
    }
    
    
    
    public function display_block($child_name, $out_type = 'html') {
        if (isset($this->blocks[$child_name])) {
            $this->blocks[$child_name]->display($out_type);
        }
    }*/
    
    
    
    
    public function display_box($box_name, $out_type = 'html') {
        if (isset($this->views[$box_name]) && is_array($this->views[$box_name])) {
            foreach ($this->views[$box_name] as $block) {
                $block->display($out_type);
            }
        }
    }
}
?>